package com.coderman.controller.business;

import com.coderman.business.service.IClassInfoService;
import com.coderman.business.service.IStudentService;
import com.coderman.common.annotation.ControllerEndpoint;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.business.ClassInfo;
import com.coderman.common.response.ResponseBean;
import com.coderman.common.vo.business.ClassVO;
import com.coderman.common.vo.student.StudentVO;
import com.coderman.common.vo.system.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(tags = "班级管理")
@RestController
@RequestMapping("/class")
public class ClassInfoController {

    @Autowired
    private IClassInfoService classInfoService;
    @Autowired
    private IStudentService studentService;

    /**
     * 用户列表
     *
     * @return
     */
    @ApiOperation(value = "班级列表", notes = "模糊查询班级列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<ClassInfo>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                     ClassVO classVo) {
        PageVO<ClassInfo> classList = classInfoService.findList(pageNum, pageSize, classVo);
        return ResponseBean.success(classList);
    }

    @ApiOperation(value = "获取员工名称", notes = "获取员工名称")
    @GetMapping("/getEmpNameById/{id}")
    @ResponseBody
    public ResponseBean<String> getEmpNameById(@PathVariable Integer id) {
        String empName = classInfoService.getEmpNameById(id);
        if(StringUtils.isEmpty(empName)){
            return ResponseBean.error("获取成功");
        }
        return ResponseBean.success(empName);
    }
    @ApiOperation(value = "获取员工名称", notes = "获取员工名称")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean<String> add(@RequestBody ClassInfo classInfo) {
        boolean flag = classInfoService.add(classInfo);
        if(!flag){
            return ResponseBean.error("保存失败");
        }
        return ResponseBean.success("保存成功");
    }

    @ApiOperation(value = "获取员工信息", notes = "获取员工信息")
    @GetMapping("/edit/{classid}")
    @ResponseBody
    public ResponseBean<ClassInfo> add(@PathVariable Integer classid) {
        ClassInfo classInfo = classInfoService.getClassById(classid);
        return ResponseBean.success(classInfo);
    }

    /**
     * 删除班级
     *
     * @param classid 班级ID
     * @return
     */
    @ApiOperation(value = "删除班级", notes = "删除班级信息，根据班级ID")
    @DeleteMapping("/delete/{classid}")
    @ResponseBody
    public ResponseBean delete(@PathVariable Integer classid) throws SystemException {
        classInfoService.deleteById(classid);
        return ResponseBean.success();
    }

    /**
     * 更新用户
     *
     * @param classid
     * @param classVO
     * @return
     */
    @ApiOperation(value = "更新班级", notes = "更新班级信息")
    @PutMapping("/update/{classid}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer classid, @RequestBody @Validated ClassVO classVO) throws SystemException {
        classInfoService.update(classid, classVO);
        return ResponseBean.success();
    }
    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有班级的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("class:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出班级excel")
    public void export(HttpServletResponse response) {
        List<ClassVO> classInfos = this.classInfoService.findAll();
        ExcelKit.$Export(ClassInfo.class, response).downXlsx(classInfos, false);
    }

    /**
     * 所有班级
     *
     * @return
     */
    @ApiOperation(value = "所有班级")
    @GetMapping("/findAll")
    @ResponseBody
    public ResponseBean<List<ClassVO>> findAll() {
        List<ClassVO> classInfos = classInfoService.findAll();
        return ResponseBean.success(classInfos);
    }
    /**
     * 导出excel
     * @param map
     */
    @ApiOperation(value = "模糊查询学生信息")
    @PostMapping("/findStuList")
    @RequiresPermissions("class:export")
    public List<StudentVO> findStuListByParams(@RequestBody Map map) {
        return studentService.findStuListByParams(map);
    }
}
